<?php
/**
 * SIS Course definitions, properties and classes
 * @package LFCMS
 * @subpackage SISCourses
 */
 
/**
 * The time of day the cache files and database should refresh
 */
$cachetime = '06:15:00';

/**
 * Include the listedcourse class definitions
 * @uses lfIncClass
 * @uses listedcourse
 */
lfIncClass('listedcourse');

/**
 * Require the configuration file for connecting to the MS SQL database
 */
require_once('cfg/ms-sql-sis-config.php');

/**
 * Determine which semesters/terms should be displayed in the schedule
 * The first digit in this code refers to the current century. For these purposes, the centuries 
 * apparently begin at the 19th century, so, even though we are currently in the 21st century, 
 * the century indicator for this code is actually 2. That is why we are subtracting 18 from 
 * the current century.
 *
 * The second and third digit in this code refer to the current two-digit year.
 *
 * The fourth digit in this code refers to the current semester. 2 = spring, 3 = summer and 
 * 4 = fall. Apparently 1 has no significance.
 *
 * When you modify these constant definitions, you should also manually clear out the params 
 * and cache directories to ensure that no outdated cache information is being pulled.
 *
 * In many cases, the curterm and maxterm constants will be the same, but for 
 * instances when more than one semester needs to be displayed on the schedule 
 * at a time, they can be defined differently.
 */

/**
 * Define the current (earliest) semester for which classes should be displayed
 */
define('CURTERM',2142); // Example: 2142 means 2014 Spring
/**	
 * Define the maximum (latest) semester for which classes should be displayed
 */
define('MAXTERM',2144); // Example: 2142 means 2014 Fall

/**
 * Define a constant to act as the default number of results that should be displayed on a single page
 */
define('DEFHM',100);

/**
 * Translate the location codes from SIS into full-text location names
 */
$course_locations = array(
/*	Example: 
	'FAUQCO'=>'Fauquier County',
	'FREDCO'=>'Frederick County',
	'LPCC'=>'Luray-Page County Center',
	'PAGECO'=>'Page County',
*/	
);
asort($course_locations);

/*
 * @global array $course_days an array to translate the SIS code for each day of the week into an AP-formatted day abbreviation
 */
global $course_days;

$course_days = array(
	'M'=>'Mon.',
	'T'=>'Tues.',
	'W'=>'Wed.',
	'R'=>'Thurs.',
	'F'=>'Fri.',
	'S'=>'Sat.',
	'U'=>'Sun.',
	'VIRT'=>'No specified day / time (online, independent study, etc.)',
);

/**
 * Set up an array to translate the SIS codes for buildings into full-text names of the buildings
 */
$course_bldgs = array(
/*	Example: 
	'I001'=>'Fairfax Hall',
	'I002'=>'Alson H. Smith Hall',
	'I003'=>'Library/Annex',
	'I005'=>'Science and Health Professions Building',
*/
);

$formlocs = (isset($_GET['LOCATION'])) ? $_GET['LOCATION'] : array();
$formterms = (isset($_GET['STRM'])) ? $_GET['STRM'] : array();

if(isset($_GET['start']) && is_numeric($_GET['start'])) {
	$start = $_GET['start'];
	unset($_GET['start']);
}
else {
	$start = 0;
}

if(isset($_GET['howmany']) && is_numeric($_GET['howmany'])) {
	$howmany = $_GET['howmany'];
	unset($_GET['howmany']);
}
else {
	$howmany = DEFHM;
}

/**
 * SIS Course class definitions
 *
 * The parameters for this class are set up to specifically match the table column names pulled from SIS
 * @package LFCMS
 * @subpackage SISCourses
 */
class sis_course {
	/**#@+
	 * Properties to match DB table column names
	 * @var string
	 */
	public $institution=NULL;
	public $term=NULL;
	public $academicsubject=NULL;
	public $xlatlongname=NULL;
	public $subject=NULL;
	public $catalog_nbr=NULL;
	public $course_title_long=NULL;
	public $class_nbr=NULL;
	public $class_section=NULL;
	public $credits=NULL;
	public $stnd_mtg_pat=NULL;
	public $lue=NULL;
	public $stime=NULL;
	public $etime=NULL;
	public $location=NULL;
	public $facility_id=NULL;
	public $instructorname=NULL;
	public $p=NULL;
	public $notetext=NULL;
	public $session_type=NULL;
	public $class_mtg_nbr=NULL;
	public $dyndescr=NULL;
	public $enrl_cap=NULL;
	public $enrl_tot=NULL;
	public $shortclassdescr=NULL;
	public $crse_i=NULL;
	public $crse_offer_nbr=NULL;
	public $strm=NULL;
	public $ses=NULL;
	public $in=NULL;
	public $sdate=NULL;
	public $edate=NULL;
	public $course_days=array();
	/**#@-*/
	/**
	 * Create the new sis_course object
	 * @param array $av an associative array of object properties and their values
	 */
	function __construct($av=array()) {
		foreach($av as $key=>$val) {
			$k = strtolower($key);
			if(property_exists($this,$k)) {
				$this->$k=$val;
			}
		}
	}
	
	/**
	 * Format some values to be more readable by humans
	 * @uses $course_days
	 * @uses formatTime()
	 */
	function formatvalues() {
		global $course_days;
		
		foreach($this as $k=>$v) {
			if(!is_numeric($v) && !empty($v)) {
				$this->$k = htmlspecialchars($v,ENT_QUOTES);
			}
		}
		$this->stime = (strtotime($this->stime)) ? formatTime(strtotime($this->stime)) : $this->stime;
		$this->stime = (!empty($this->stime) && strtolower($this->stime) != 'mid' && strtolower($this->stime) != 'midnight') ? $this->stime : NULL;
		$this->etime = (strtotime($this->etime)) ? formatTime(strtotime($this->etime)) : $this->etime;
		$this->etime = (!empty($this->etime) && strtolower($this->etime) != 'mid' && strtolower($this->etime) != 'midnight') ? $this->etime : NULL;
		
		$this->stime = (substr($this->stime,-4) == substr($this->etime,-4)) ? substr($this->stime,0,(strlen($this->stime)-5)) : $this->stime;
		
		$days = array();
		$this->stnd_mtg_pat = strtoupper($this->stnd_mtg_pat);
		if($this->stnd_mtg_pat != 'VIRT') {
			$through = false;
			for($i=0;$i<strlen($this->stnd_mtg_pat);$i++) {
				if($this->stnd_mtg_pat[$i] == '-') {
					$through = true;
				}
				else {
					$days[] = $this->stnd_mtg_pat[$i];
				}
			}
		}
		else {
			$days = array('Web-based');
		}
		foreach($days as $k=>$d) {
			$days[$k] = (array_key_exists($d,$course_days)) ? $course_days[$d] : '&nbsp;';
		}
		$days = array_values($days);
		if(count($days) == 1) {
			$this->stnd_mtg_pat = $days[0];
		}
		elseif($through && count($days) == 2) {
			$this->stnd_mtg_pat = $days[0].' through '.$days[1];
		}
		else {
			$ld = array_pop($days);
			$this->stnd_mtg_pat = implode(', ',$days).' and '.$ld;
		}
	}
}
?>
